Repository navigation
feat(agents): 用量补齐会话分位数、每日消息数、上下文占用、额度历史和按目录筛选,插件可上报账号与用途,刷新额度、自定义价格、汇率和额度提醒由插件实现 - #267
Draft
xiaoyumuxi wants to merge 1 commit into
Draft
xiaoyumuxi wants to merge 1 commit into
xiaoyumuxi wants to merge 1 commit into
Conversation
xiaoyumuxi
force-pushed
the
feat/agent-usage-insights
branch
from
December 3, 2026 16:52
5a7b90a to
d80d8ee
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
关联
agentUsage.notDeclared拒绝,宿主据此提示“该插件不支持这项功能”。Core 对具体 Agent、CLI、模型名零特判,这些功能都不联网。“清除用量”用户未定,本 PR 不做,契约里也不预留。改动
都是 API-45、
agents能力 0 内的补充,不升版本。插件后端新增两个可选的普通插件函数(额度、汇率),没有新增插件可调用的宿主方法或通知;四个 SDK 都没有这部分的类型化代码,不改代码。一、插件在调用上报告的新字段
scan的calls[]新增可选字段:account:发起调用的账号或组织,插件自己的标签,1–256 个可见 ASCII 字节、不含空格;purpose:调用的用途,比如主对话、子代理、生成标题,同样是标签;cancelled:中途取消。取消不算失败,不能同时带errorCategory;toolUses:响应要求的工具调用次数,最多 1000000。cancelledCalls、toolUses;取消的调用不计入速度和首字时间。二、统计(
agentSessions.stats)groupBy、splitBy新增account、purpose;stats和calls新增筛选accounts、purposes、cancelled,search也匹配账号和用途。每日缓存的单元按新标签拆分,缓存结果和逐条计算一致。stats和calls新增筛选cwds:按会话的工作目录整段精确匹配,/work/atlas不会连带/work/atlas-2或/work/atlas/sub;空字符串代表没记录目录的会话;取值和cwd分组的键一致。至多 64 个、每个至多 4096 字节(和agentSessions.list的cwdUnder同一上限),超出沿用agentSessions.invalidFilter拒绝。宿主的项目卡改用它,不再把路径塞进search做子串匹配。每日缓存不记目录,所以带cwds(和带search一样)时概览逐条读取,不用已存的天。overview.speeds: true:同一次读取也回答speeds,规则和不带概览时的速度排名相同(至少 100 个输出令牌、按模型取中位数、快的在前、并列按模型名)。实现成本低:每日缓存的单元本来就按模型存着排名要用的速度样本,多建一份按模型的投影、复用已有的分块中位数即可,不另读调用;不传时回答不带speeds,和以前一样。桌面端因此可以去掉为速度排名额外发的groupBy: "all"请求。sessions: {top?}:按会话的统计。count:范围内有调用的会话数;calls、tokens、uncachedTokens、costUSD、durationMs各给{sessions, mean, p50, p90, p99, max};ceil(p × n / 100)个值,每个分位数都是某个真实会话的值;costUSD只算至少有一次有价格调用的会话;durationMs从第一次调用开始到最后一次调用结束;costliest:最贵的至多top个会话(上限 50),并列按 Agent 和会话 ID 排序。messages: true:按小时或天的消息数(插件报告的可搜索片段,只算会话自己的文件,复制的文件和子代理文件不重复)。其他分组以新错误码agentSessions.invalidMessagesGroup拒绝。按天分组、范围一年就能画 53 周的热力图。cwd分组时,键带和会话列表相同的category,宿主可以用同一规则按项目归类。currency {code, perUSD, atMs}。费用始终以美元给出,由宿主换算显示。三、请求列表(
agentSessions.calls)cwds,规则同上。generation、size:和call.range.start一起交给agentSessions.page,就能打开这次调用所在的位置;context {tokens, window?, share?}:这次调用的上下文占用。tokens是提示的令牌数(输入、缓存读取、缓存写入);window取自 Core 本次运行最近一次读到的插件模型列表里该模型的contextWindow;没有窗口时只给tokens。按调用顺序画出来就是一个会话的上下文曲线。agentSessions.read {agent, path, range},不在列表里批量读,避免每页触发一批插件调用。四、用量功能(
agentUsage.*,插件实现)usage {rateLimits?, exchangeRates?, customPrices?, rateLimitAlerts?},至少声明一项。前两项是插件函数,Agent 因此需要后端和onAgent:<id>。目录条目带usage,每项是布尔值,不给函数 ID。agentUsage.read{}{currency?, exchangeRates, agents}agentUsage.setPrice{pluginID, agentID, model, price}{}agentUsage.setAlert{pluginID, agentID, window, thresholds}{}agentUsage.setCurrency{currency}{}agentUsage.refreshRateLimits{pluginID, agentID}{rateLimits}agentUsage.refreshExchangeRates{pluginID, agentID, currencies}{exchangeRates}agentUsage.rateLimitHistory{pluginID, agentID, window?, sinceMs?, untilMs?}{windows}{agentID}调插件的rateLimits函数(15 秒),回答形状和会话事件的rateLimits一样,Core 存为这个 Agent 最新的额度快照。price是{input, output, cacheRead?, cacheWrite?, reasoning?},单位是美元/百万令牌,每项 0 到 100000;传null删除。每个 Agent 最多 256 个模型。这个 Agent 的该模型调用先用它,再用价格表;没有价格表时也生效。改价后统计、每日缓存和请求行立刻按新价格计算。{agentID, base: "USD", currencies}调插件的exchangeRates函数,回答必须正好是{rates},只能包含请求的货币,取值大于 0、不超过 10000000。Core 保存每种货币最近的汇率和来源 Agent。setCurrency只接受已有汇率的货币,传null或USD表示美元。alert {thresholds, reached?};新快照达到更高阈值时记日志agent.usage.alert_hit,怎么提醒用户由宿主决定。agentSessions.forget删除该 Agent 的历史。agentSessions.configure新增dataDirectory,必须是平台应用数据目录里的绝对路径,不能是缓存目录。设置存usage-settings.json,历史存rate-limit-history.json,都仅所有者可读写,经临时文件整体替换。读不出的设置文件改名为usage-settings.unreadable.json保留,不覆盖。价格和提醒在 forget 后保留。不给dataDirectory时,除refreshRateLimits外都以agentUsage.notConfigured拒绝。五、错误码与日志
agentUsage.notDeclarednot_supportedagentUsage.notConfiguredinvalid_requestdataDirectoryagentUsage.unknownAgententry_not_foundagentUsage.notGrantedpermission_deniedagentUsage.userActionRequiredpermission_deniedagentUsage.backendUnavailableinvalid_requestagentUsage.pluginFailedprocess_failedagentUsage.invalidAnswerparse_failedagentUsage.invalidPriceinvalid_requestagentUsage.invalidAlertinvalid_requestagentUsage.invalidCurrencyinvalid_requestagentUsage.noExchangeRateentry_not_foundagentUsage.invalidRangeinvalid_requestagentUsage.saveFailedpermission_deniedagentSessions.invalidMessagesGroupinvalid_request已有的
agentSessions.invalidFilter含义不变(筛选条件超出上限),只把cwds的条数和长度纳入,提示文字随之补上目录。新日志事件:
agent.usage.file_failed、agent.usage.price_changed、agent.usage.rate_limits_updated、agent.usage.exchange_rates_updated、agent.usage.plugin_failed、agent.usage.alert_hit。只记插件、Agent、阈值、计数和错误类别,不记模型名、价格、账号、路径。agent.usage.file_failed的stage取值有read、write、cleanup(写失败后临时文件也删不掉)、setAside。六、文档
agents.plugin.json加上usage;invalid-manifests.json新增两个拒绝用例;rust/contracts/rust-core-api.md:命令表和新的 Usage features 一节;会话缓存布局版本写成 5;domains/agents.md(中英);agents0 行;agentSessions.subagents原来先拿价格锁再拿读连接锁,和统计的顺序相反;本 PR 让价格可以随时被改,所以统一成先读连接后价格,避免死锁。七、合并 agent-runtime 里重复的辅助函数
不改行为,只把多份实现合成一份:
agent-runtime/src/files.rs:read_bounded:原来资源库和用量设置各有一份。合并后统一为:不存在回None;不是普通文件、读失败回ReadFailure::Io(错误类别);超过上限回ReadFailure::TooLarge,先看文件大小,最多读上限 + 1字节。用量设置记日志用ReadFailure::kind()取错误类别。replace_file:原子替换文件,合并了资源库的write_replacing和用量设置的write_private。新临时文件先以仅所有者可读写创建,目标已存在时再改成原文件的权限,写盘、改名覆盖;失败时删掉临时文件,删不掉才回调调用方记日志(资源库记agent.library.cleanup_failed,用量记agent.usage.file_failed的cleanup)。资源库保留一个三行的write_replacing,只负责绑定自己的日志。sibling_temporary:临时文件名,原来两份。lib.rs的refusal和require_user:refusal原来在 records、library、live 各一份;require_user原来在 live、library、用量各一份,合成一个只检查“是不是用户发起的请求”的函数,拒绝内容由调用方给。三个领域的错误码不变,仍是agents.userActionRequired、agentLibrary.userActionRequired、agentUsage.userActionRequired,提示文字也不变。library/backups.rs的write_private是“新建一个仅所有者可读写的文件”(不替换已有文件),和上面的原子替换不是同一件事,保留。tests/files.rs覆盖合并后的读取上限、非文件、替换后不留临时文件、失败时清理;原有资源库测试(保留 0640 权限、新文件 0600)和用量测试(设置文件 0600)照常通过。对宿主的影响
PINNED_CORE_REVISION升到本 PR 的合入提交。要做的有:agentSessions.configure传dataDirectory(Application Support 下的目录,不能放进 app bundle,也不能是缓存目录);usage显示或禁用刷新额度、自定义价格、货币、额度提醒;收到agentUsage.notDeclared时提示“该插件不支持这项功能”;cwd+category)归类、上下文曲线、额度历史图、请求定位(agentSessions.page);alert.reached的变化后通知用户;cwds精确筛选;概览请求带overview.speeds: true,去掉为速度排名额外发的groupBy: "all"统计请求。cacheHitRate。scan在会话文件有记录时报告account、purpose、cancelled、toolUses;read的模型带contextWindow,上下文占比才算得出来;usage:customPrices: true、rateLimitAlerts: true;能按需读到额度时实现rateLimits;要提供汇率时实现exchangeRates(数据从哪来由插件决定);验证
本机 macOS arm64,基于 main 5364983(含 #263):
cargo test --locked -p lithe-agent-sessions -p lithe-agent-runtime -p lithe-plugin-agents -p lithe-plugin-manifest -p lithe-diagnosticscargo test --locked -p lithe-corecargo clippy --locked -p lithe-agent-sessions -p lithe-agent-runtime -p lithe-plugin-agents -p lithe-core -p lithe-diagnostics -p lithe-plugin-manifest --all-targets -- -D warnings、cargo fmt --all -- --checkLIBSQLITE3_SYS_USE_PKG_CONFIG=1 ZSTD_SYS_USE_PKG_CONFIG=1 PKG_CONFIG_ALLOW_CROSS=1 cargo clippy --locked --target x86_64-pc-windows-msvc -p <crate> --all-targets -- -D warningsverify-docs.mjsgenerate-log-events.mjs --checkverify-rust-core-comments.shverify-platform-tests.mjssdk-e2e.mjs新测试覆盖:
cwds整段匹配:/work/atlas不带上/work/atlas-2和子目录,空字符串选没记录目录的会话;概览在已存天之后带cwds仍和逐条计算一致;stats、calls两边生效,超过 64 个或超过 4096 字节被agentSessions.invalidFilter拒绝;overview.speeds:按天(第二次从已存的天读)、按小时、带搜索、带模型筛选四种情况都和逐条算的速度排名一致;不传时不带speeds;运行时里概览的speeds和不带概览的统计一致;notDeclared;非用户请求被拒;currency;